package com.leetCode.zero;

import java.lang.reflect.Method;
import java.net.InterfaceAddress;
import java.util.HashMap;
import java.util.Map.Entry;
import java.util.Set;

public class Problem07 {
	public static void main(String[] args) throws Exception {
		Solution s = getInstance();
		HashMap<Integer, Integer> hm = new HashMap<>();
		hm.put(123, 321);
//		Set<Entry<Integer,Integer>> entrySet = hm.entrySet();
//		for (Entry<Integer, Integer> entry : entrySet) {
//			int key = entry.getKey();
//			int value = entry.getValue();
//			if(value == s.reverse(key)){
//				System.out.println(key+":"+s.reverse(key)+"===>"+true);
//			}else{
//				System.out.println(key+":"+s.reverse(key)+"===>"+false);
//			}
//		}
		int k = -2147483421;
		System.out.println(k+"-->"+s.reverse(k));
		System.out.println(Integer.MAX_VALUE+":"+Integer.MIN_VALUE);
		System.out.println(964632435*10);
	}

	class Solution {
		public int reverse(int x) {
			int result = 0,temp = x;
			while(x!=0){
				if((x>0 && result!=0 && Integer.MAX_VALUE/result <10) || (x<0 && result!=0 && Integer.MIN_VALUE/result <10 && Integer.MIN_VALUE/result >0)){
					return 0;
				}
				result = result*10+x%10;
				
				x = x/10;
			}
			if((temp>0 && result<0 )|| (temp<0 && result >0)){
				return 0;
			}
	        return result;
	    }
		public int reverse1(int x) {
			String a = ""+x;
			boolean isNeg = false;
			if(a.startsWith("-")){
				a = a.substring(1);
				isNeg = true;
			}
			String reverse = new StringBuffer(a).reverse().toString();
			reverse = isNeg?"-"+reverse:reverse;
			int result = 0;
			try{	
				result = Integer.parseInt(reverse);
			}catch (NumberFormatException e) {
				result = 0;
			}
	        return result;
	    }
	}

	static Solution getInstance() throws Exception {
		String className = Thread.currentThread().getStackTrace()[1].getClassName();
		Class<?> forName = Class.forName(className);
		Method method = forName.getMethod("newS", null);
		Solution solution = (Solution) method.invoke(forName.newInstance(), null);
		return solution;
	}

	public Solution newS() {
		return new Solution();
	}
}
